package com.example.demo.service.weCon.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.config.WeConConfig;
import com.example.demo.config.WeConConfig.ContactsManagement;
import com.example.demo.entity.DTO.WeConDepartmentDTO;
import com.example.demo.entity.QUERY.WeConDepartmentQuery;
import com.example.demo.service.weCon.ContactsManagementService;
import com.example.demo.service.weCon.WeConService;
import com.example.demo.utils.WeChatHttpClient;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author jack
 * @data 2021/8/13 10:38 上午
 */
@Service
public class ContactsManagementServiceImpl implements ContactsManagementService {

  @Autowired
  private WeConService weConService;

  @Override
  public Integer getActiveStat(String date) {
    // 获取 获取企业活跃成员数url
    String url = weConService.generateWeConUrl(WeConConfig.ContactsManagement.GET_ACTIVE_STAT,
        this.getWeConFunctionType());
    // 设置请求参数
    JSONObject param = new JSONObject();
    if (StringUtils.isBlank(date)) {
      // date 为空 设置为当天
      date = LocalDate.now().toString();

    }
    param.put("date", date);
    Integer num = Integer.parseInt(WeChatHttpClient.doPostSingleReturnByParams(url, param));
    return num;
  }

  @Override
  public String getJoinQrcode() {
    String url = weConService
        .generateWeConUrl(ContactsManagement.GET_JOIN_QRCODE, this.getWeConFunctionType());
    // 设置请求参数
    return WeChatHttpClient.doGetSingleReturn(url);
  }

  @Override
  public List<WeConDepartmentDTO> listDepartment(Integer id) {
    String url = weConService
        .generateWeConUrl(ContactsManagement.LIST_DEPARTMENT, this.getWeConFunctionType());
    // 设置请求参数
    HashMap<String, String> paramMap = new HashMap<>();
    if (id != null) {
      paramMap.put("id", id.toString());
    }
    return WeChatHttpClient.doGetListByMap(url, paramMap, WeConDepartmentDTO.class);
  }

  @Override
  public void createDepartment(WeConDepartmentQuery weConDepartmentQuery) {
    String url = weConService
        .generateWeConUrl(ContactsManagement.CREATE_DEPARTMENT, this.getWeConFunctionType());
    WeChatHttpClient
        .doPostSingleReturnByParams(url, (JSON) JSONObject.toJSON(weConDepartmentQuery));
  }

  @Override
  public void updateDepartment(WeConDepartmentQuery weConDepartmentQuery) {
    String url = weConService
        .generateWeConUrl(ContactsManagement.UPDATE_DEPARTMENT, this.getWeConFunctionType());
    WeChatHttpClient
        .doPostSingleReturnByParams(url, (JSON) JSONObject.toJSON(weConDepartmentQuery));
  }

  @Override
  public void deleteDepartment(Integer id) {
    String url = weConService
        .generateWeConUrl(ContactsManagement.DELETE_DEPARTMENT, this.getWeConFunctionType());
    HashMap<String, String> paramMap = new HashMap<>();
    paramMap.put("id", id.toString());
    WeChatHttpClient.doGetByMap(url, paramMap);
  }

  public static void main(String[] args) {
    System.out.println(LocalDate.now().toString());
  }

}
